package yl.hs.bmipfull.controllers.poundroom;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.pojo.laboratory.ProductInfoEntity;
import yl.hs.bmipfull.pojo.production.ProductShipOrderEntity;
import yl.hs.bmipfull.service.laboratory.IProductInfoService;
import yl.hs.bmipfull.service.laboratory.IProductOverageService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.service.system.IDataDictService;
import yl.hs.bmipfull.service.vehiclemanage.ITruckInfoService;
import yl.hs.bmipfull.utils.datastate.ShipControlState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 作用:   <br/>
 * 创建时间: 2021-06-21 16:15 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "磅房Web接口", tags = "过磅管理——商砼过磅操作")
@RequestMapping(value = "/api/concreteweight/")
@RestController
public class ConcreteWeighControllers extends ControllerBase {
    @Autowired
    IProductionLineService service;
    @Autowired
    IDataDictService diSvc;
    @Autowired
    IProductionTaskService productionTaskService;
    @Autowired
    ITruckInfoService servicecar;
    @Autowired
    ISaleContractService contractservice;
    @Autowired
    IProductShipOrderService productshiporderservice;
    @Autowired
    IProductShipOrderDao productshiporderdao;
    @Autowired
    IProductInfoService productinfoservice;
    @Autowired
    ITransTaskService transTaskService;
    @Autowired
    IProductionLineService lineService;
    @Autowired
    IProductOverageService ProductOverageservice;

    @ApiOperation(value = "查询生产线信息表", notes = "查询生产线信息表")
    @RequestMapping(value = {"/selectLine/{key}", "/selectLine"}, method = RequestMethod.GET)
    public WebResult queryListLine(@PathVariable(required = false, name = "key") String key) {
        try {
            return success("查询成功", service.queryList(key));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据字典编号查询字典数据项", notes = "根据字典编号查询字典数据项")
    @RequestMapping(path = "/type/{typeSn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult queryItemByTypeSN(@PathVariable("typeSn") String typeSn) {
        try {
            var data = diSvc.findDictItemByTypeSN(typeSn);
            if (data != null && data.size() > 0) {
                return success("ok", data);
            } else {
                return success("没有找到任何信息");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询生产中的生产任务", notes = "查询生产中的生产任务")
    @RequestMapping(path = "/productTask/{sn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult queryProductTask(@PathVariable("sn") String sn) {
        try {
            var data = productionTaskService.queryTaskSc(sn);
            if (data != null && data.size() > 0) {
                return success("ok", data);
            } else {
                return success("没有找到任何信息");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询车辆信息", notes = "查询车辆信息")
    @RequestMapping(value = "/car/{mixTruck}", method = RequestMethod.GET)
    public WebResult queryListCar(@ApiParam(value = "查询条件") @PathVariable String mixTruck) {
        try {
            PageQuery pagequery = new PageQuery();
            if(mixTruck.equals("tankTruck")){
                mixTruck = "sharing";
            }
            String finalMixTruck = mixTruck;
            pagequery.setQuery(new HashMap<String, Object>() {
                {
                    put("useType", finalMixTruck);
                }
            });
            return success("查询成功", servicecar.queryList(pagequery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "保存车辆信息", notes = "查询/更新车辆信息信息")
    @RequestMapping(value = "/updateCar", method = RequestMethod.PUT)
    public WebResult saveCar(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = servicecar.saveTruckInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功",result);
            } else {
                return fail("数据未保存",0);
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆编号查询车辆信息", notes = "根据车辆编号查询车辆信息")
    @RequestMapping(value = "/querybytruckcode/{truckCode}", method = RequestMethod.GET)
    public WebResult queryByTruckCode(@ApiParam(value = "提交信息") @PathVariable String truckCode) {
        try {
            var data = servicecar.queryByTruckCode(truckCode);
            if (data != null) {
                return success("ok", data);
            } else {
                return success("没有找到任何信息");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询销售合同信息", notes = "根据销售合同信息编号查询销售合同信息")
    @RequestMapping(value = "/contract/{sn}", method = RequestMethod.GET)
    public WebResult findContractBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", contractservice.findBySn(sn));
    }

    @ApiOperation(value = "保存产品发货单", notes = "查询/更新产品发货单信息")
    @RequestMapping(value = "/productshiporder", method = RequestMethod.PUT)
    public WebResult saveProductShipOrder(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        ProductShipOrderEntity result;
        try {
            result = productshiporderservice.saveProductShipOrder(submitModel.getData(), submitModel.getSn());
            if (result != null) {
                return success("保存成功",result);
            } else {
                return fail("数据未保存",result);
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "保存产品发货单", notes = "查询/更新产品发货单信息")
    @RequestMapping(value = "/saveOutWeight", method = RequestMethod.PUT)
    public WebResult saveOutShipOrder(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel){
        try{
            var order = productshiporderservice.saveOutWeight(submitModel);
            if(order==null){
                return  fail("数据保存失败");
            }
            else
                return success("数据保存成功",order);
        }
        catch (Exception ee){
            return  error(ee,0);
        }
    }

    @ApiOperation(value = "查询产品发货单", notes = "根据产品发货单编号查询产品发货单")
    @RequestMapping(value = "/productshiporder/{sn}", method = RequestMethod.GET)
    public WebResult findProductShipOrderBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", productshiporderservice.findBySn(sn));
    }

    @ApiOperation(value = "查询产品发货单", notes = "根据产品发货单状态，条数，排序字段查询产品发货单")
    @RequestMapping(value = "/productshiporder", method = RequestMethod.POST)
    public WebResult findProductShipOrderByState(@ApiParam(value = "发货单状态") @RequestParam short state, @ApiParam(value = "返回数据条数") @RequestParam int top, @ApiParam(value = "排序字段") @RequestParam String sort, @ApiParam(value = "查询条件") @RequestParam Object key,@RequestParam(required = false) String productCateSN) {
        return success("查询成功", productshiporderservice.findByState(state, top, sort, key,productCateSN));
    }

    @ApiOperation(value = "查询产品发货单", notes = "查询产品发货单")
    @RequestMapping(value = "/{productCateSn}", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery,@ApiParam(value = "发货单类型") @PathVariable String productCateSn ) {
        try {
            return success("查询成功", productshiporderservice.queryList(pageQuery,productCateSn));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆编号查询最新发货单信息", notes = "根据车辆编号查询最新发货单信息")
    @RequestMapping(value = "/befor/{truckCode}", method = RequestMethod.GET)
    public WebResult queryList(@ApiParam(value = "查询条件") @PathVariable String truckCode) {
        try {
            return success("查询成功", productshiporderservice.findListByTruckCode(truckCode));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆编号以及发货类型查询该车上次单据信息", notes = "根据车辆编号以及发货类型查询该车上次单据信息")
    @RequestMapping(value = "/byordertask/{productCateSN}/{truckCode}", method = RequestMethod.GET)
    public WebResult findByOrderType(@ApiParam(value = "单据类型") @PathVariable String productCateSN, @ApiParam(value = "车辆自编号") @PathVariable String truckCode) {
        try {
            return success("查询成功", productshiporderservice.findByOrderType(productCateSN, truckCode));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询生产任务下的最新单据信息", notes = "查询生产任务下的最新单据信息")
    @RequestMapping(value = "/bytasksn/{productCateSN}/{taskSN}", method = RequestMethod.GET)
    public WebResult findByTaskSn(@ApiParam(value = "单据类型") @PathVariable String productCateSN, @ApiParam(value = "生产任务sn") @PathVariable String taskSN) {
        try {
            return success("查询成功", productshiporderservice.findByTaskSn(productCateSN, taskSN));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆编号查询是否有暂存信息", notes = "根据车辆编号查询是否有暂存信息")
    @RequestMapping(value = "/bytruckcode/{truckCode}/{state}", method = RequestMethod.GET)
    public WebResult findByTruckCode(@ApiParam(value = "车辆自编号") @PathVariable String truckCode, @ApiParam(value = "发货单状态") @PathVariable short state) {
        try {
            List<ProductShipOrderEntity>  productorder = productshiporderservice.findByTruckCodeState(truckCode, state);
            return success("查询成功", productorder);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据产品名称查询产品信息", notes = "根据产品名称查询产品信息")
    @RequestMapping(value = "/productinfo/{productName}", method = RequestMethod.GET)
    public WebResult findProductInfoByName(@ApiParam(value = "产品名称") @PathVariable String productName) {
        PageQuery pagequery = new PageQuery();
        pagequery.setQuery(new HashMap<String, Object>() {
            {
                put("productName", productName);
            }
        });
        try {
            List<ProductInfoEntity> productInfo = productinfoservice.queryList(pagequery);
            return success("查询成功", productInfo);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆编号查询派车信息", notes = "根据车辆编号查询派车信息")
    @RequestMapping(value = "/transtask/{truckCode}", method = RequestMethod.GET)
    public WebResult findTranstaskByCode(@ApiParam(value = "车辆编号") @PathVariable String truckCode) {
        try {
            return success("查询成功", transTaskService.queryListByTaskCar(truckCode));
        } catch (Exception ee) {
            return fail("查询失败，查询条件异常");
        }
    }

    @ApiOperation(value = "根据派车id更改派车状态", notes = "根据派车id更改派车状态")
    @RequestMapping(value = "/updatestate/{state}/{sn}", method = RequestMethod.GET)
    public WebResult updateStateBysn(@ApiParam(value = "派车状态") @PathVariable short state, @ApiParam(value = "派车id") @PathVariable String sn) {
        var result = transTaskService.updateBySn(state, sn);
        if (result > 0) {
            return success("成功",result);
        } else {
            return error("失败",0);
        }
    }

    @ApiOperation(value = "根据生产任务sn查询生产任务信息", notes = "根据生产任务sn查询生产任务信息")
    @RequestMapping(value = "/findtaskbysn/{sn}", method = RequestMethod.GET)
    public WebResult findTaskBySn(@ApiParam(value = "生产任务编号") @PathVariable String sn) {
        try {
            return success("查询成功", productionTaskService.findBySn(sn));
        } catch (Exception ee) {
            return fail("查询失败，查询条件异常");
        }
    }

    @ApiOperation(value = "根据单据sn查询单据信息", notes = "根据单据sn查询单据信息")
    @RequestMapping(value = "/findproductbysn/{sn}", method = RequestMethod.GET)
    public WebResult findProductBySN(@ApiParam(value = "生产任务编号") @PathVariable String sn) {
        try {
            return success("查询成功", productshiporderservice.findBySn(sn));
        } catch (Exception ee) {
            return fail("查询失败，查询条件异常");
        }
    }

    @ApiOperation(value = "保存运输任务,商砼可以拆为2条派车任务", notes = "查询/更新运输任务,商砼可以拆为2条派车任务信息")
    @RequestMapping(value = "/savetranstask", method = RequestMethod.PUT)
    public WebResult saveTransTask(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = transTaskService.saveTransTask(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "根据车辆类型查询最大排序", notes = "根据车辆类型查询最大排序")
    @RequestMapping(value = "/findsortcodeamx/{useType}", method = RequestMethod.GET)
    public WebResult findSortCodeaMax(@ApiParam(value = "车俩类型") @PathVariable String useType) {
        try {
            return success("查询成功", servicecar.findMaxSortCode(useType));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询生产线信息表", notes = "根据生产线信息表编号查询生产线信息表")
    @RequestMapping(value = "/line/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", lineService.findBySn(sn));
    }

    @ApiOperation(value = "保存退剩转信息", notes = "查询/更新退剩转信息信息")
    @RequestMapping(value = "/saveoverage", method = RequestMethod.PUT)
    public WebResult saveOverage(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = ProductOverageservice.saveProductOverage(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功",result);
            } else {
                return fail("数据未保存",0);
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据车辆自编号跟新余料信息", notes = "根据车辆自编号跟新余料信息")
    @RequestMapping(value = "/updateoverbycode/{truckCode}", method = RequestMethod.GET)
    public WebResult updateOverByCode(@ApiParam(value = "车辆编号") @PathVariable String truckCode) {
        Integer result;
        try {
            result = ProductOverageservice.updateOverByTruckCode(truckCode);
            if (result > 0) {
                return success("修改成功",result);
            } else {
                return fail("修改失败",0);
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询所有产品信息", notes = "查询所有产品信息")
    @RequestMapping(value = "/selectAll/productInfo", method = RequestMethod.GET)
    public WebResult selectAllProductInfo() {
      return success("查询成功",productinfoservice.queryAll());
    }
    @ApiOperation(value = "模糊查询生产中的生产任务",notes = "模糊查询生产中的生产任务")
    @RequestMapping(value = "/querytasktj/{catesn}/{condition}")
    public WebResult queryProductTaskByTJ(@ApiParam(value = "产品编号") @PathVariable String catesn,@ApiParam(value = "查询字符串") @PathVariable String condition){
        try {
            var result = productionTaskService.queryListByCondition(catesn,condition);
            return success("查询成功",result);
        }catch (Exception ee){
            return error(ee);
        }

    }
    @ApiOperation(value = "获取合同控制",notes = "获取合同控制")
    @RequestMapping(value = "/checkContractControl/{contractSN}/{netWeight}",method = RequestMethod.GET)
    public WebResult checkContractControl(@ApiParam(value = "合同编号") @PathVariable String contractSN,@ApiParam(value = "发货净重") @PathVariable BigDecimal netWeight){

       try{
           var result = transTaskService.checkContractControl(contractSN,netWeight);
           if(result == ShipControlState.ALLOW)
           {
               return success(result.message,200);
           }else if(result==ShipControlState.AMOUNTOUT || result==ShipControlState.SHIPMENTOUT || result==ShipControlState.TIMEOUT){
               return success(result.message,400);
           }else{
               return success(result.message,500);
           }
       }
       catch (Exception ee){
           return error(ee);
       }
    }
}
